/*
 * Moseycode
 * Copyright (C) 2008  Tom Gibara
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package com.tomgibara.mosey.portal;

/**
 * Records a set of codewords combined with the portal to which they may be
 * decoded. This class holds a reference to any codeword array that
 * set-on/returned-by any of its methods. These arrays must not be modified
 * for as long as they remain associated with an instance of this class.
 * 
 * @author Tom Gibara
 *
 */

public class CodedPortal {

	// fields
	
	/**
	 * The portal to which the codewords are decoded. May be lazily instantiated
	 * and may be null.
	 */
	
	private Portal mPortal = null;
	
	/**
	 * An array of codewords that decodes to the portal. May be lazily
	 * instantiated and may be null.
	 */
	
	private int[] mCodewords = null;

	// constructors
	
	/**
	 * Construct a coded portal with no specific codewords or portal.
	 */
	
	public CodedPortal() {
	}

	/**
	 * Construct a coded portal for a portal. Codewords will be
	 * generated from the supplied portal.
	 * 
	 * @param portal the portal from which the codewords should be generated
	 */
	
	public CodedPortal(final Portal portal) {
		this.mPortal = portal;
	}
	
	/**
	 * Construct a coded portal from an array of codewords. The portal will be
	 * decoded from the supplied codewords.
	 * 
	 * @param codewords an array of 8 codewords
	 */
	
	public CodedPortal(final int[] codewords) {
		this.mCodewords = codewords;
	}
	
	// accessors

	/**
	 * The codewords that have been supplied directly, or generated from a
	 * supplied portal.
	 * 
	 * @return the codewords for the coded portal, may be null
	 */
	
	public int[] getCodewords() {
		if (mCodewords == null) {
			if (mPortal == null) return null;
			mCodewords = PortalSymbology.codewords(mPortal);
		}
		return mCodewords;
	}
	
	/**
	 * Changes the codewords on this coded portal. This will cause the portal
	 * to be recalculated.
	 * 
	 * @param codewords an array of 8 codewords, may be null
	 */
	
	public void setCodewords(final int[] codewords) {
		this.mCodewords = codewords;
		mPortal = null;
	}
	
	/**
	 * The portal that has been specified directly, or decoded from the
	 * supplied codewords.
	 * 
	 * @return the portal of this coded portal, or null
	 */
	
	public Portal getPortal() {
		if (mPortal == null) {
			if (mCodewords == null) return null;
			mPortal = PortalSymbology.portal(mCodewords);
		}
		return mPortal;
	}
	
	/**
	 * Changes the portal on this coded portal. This will cause the array of
	 * codewords to be regenerated.
	 * 
	 * @param portal a portal, may be null
	 */
	
	public void setPortal(final Portal portal) {
		this.mPortal = portal;
		mCodewords = null;
	}
	
}
